﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Controller;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using System.IO;


public partial class NPS_app_Monitoring_SummaryReason : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadForm();
        }
    }

    private void refreshPanel()
    {
        pnlOptCheck.Visible = false;
        pnlOptRadio.Visible = false;
    }

    private void loadForm()
    {
        ddlBatch.DataSource = Batch;
        ddlBatch.DataBind();
        ddlBatch.SelectedIndex = 0;

        ddlView.Items.Clear();
        ddlView.Items.Add(new ListItem(V_Total, V_Total));
        ddlView.Items.Add(new ListItem(V_Class, V_Class));
        ddlView.Items.Add(new ListItem(V_Segment, V_Segment));
        ddlView.Items.Add(new ListItem(V_Company, V_Company));
        ddlView.Items.Add(new ListItem(V_Reason, V_Reason));
        ddlView.SelectedIndex = 0;

        rblShow.Items.Clear();
        rblShow.Items.Add(new ListItem(type_Promoter, type_Promoter));
        rblShow.Items.Add(new ListItem(type_Detractor, type_Detractor));
        rblShow.SelectedIndex = 0;

        chkShow.Items.Clear();
        chkShow.Items.Add(new ListItem(type_Promoter, type_Promoter));
        chkShow.Items.Add(new ListItem(type_Passive, type_Passive));
        chkShow.Items.Add(new ListItem(type_Detractor, type_Detractor));
        chkShow.Items[0].Selected = true;
        chkShow.Items[1].Selected = true;
        chkShow.Items[2].Selected = true;

        //loadPanel(V_Reason);
        loadPanel(V_Total);
        showChart();

    }

    private void loadPanel(string panel)
    {
        refreshPanel();
        if (panel.Equals(V_Reason))
        {
            pnlOptRadio.Visible = true;
        }
        else
        {
            pnlOptCheck.Visible = true;
        }
    }

    private void showChart()
    {
        string Charttype = ddlView.SelectedValue;
        int year = Convert.ToInt32(ddlBatch.SelectedValue);

        if (Charttype.Equals(V_Reason))
        {
            Controller.NPSTypeController tc = new Controller.NPSTypeController();
            int type = tc.getNPSType(rblShow.SelectedValue).iID;
            getReasonSummary(Convert.ToInt32(ddlBatch.SelectedValue.ToString()), type);
        }
        else
        {
            bool isPromoter = chkShow.Items[0].Selected;
            bool isPassive = chkShow.Items[1].Selected;
            bool isDetractor = chkShow.Items[2].Selected;

            if (Charttype.Equals(V_Total))
            {
                getTotalSummary(year, isPromoter, isPassive, isDetractor);
            }
            else if (Charttype.Equals(V_Company))
            { getCompanySummary(year, isPromoter, isPassive, isDetractor); }
            else if (Charttype.Equals(V_Class))
            { getClassSummary(year, isPromoter, isPassive, isDetractor); }
            else if (Charttype.Equals(V_Segment))
            { getSegmentSummary(year, isPromoter, isPassive, isDetractor); }
            else
            { }
        }
    }

    private static int initialheight = 180;
    private static int intervalheight = 30;

    private void getTotalSummary(int year, bool isPromotore, bool isPassive, bool isDetractor)
    {
        pChart.Controls.Clear();
        NPSCustomerContactController ncc = new NPSCustomerContactController();

        //chart initialize
        Chart chart = new Chart();
        //chart.ImageLocation = "~/tempImages/ChartPic_#SEQ(300,3)";
        chart.ImageType = ChartImageType.Png;
        chart.Titles.Add(new Title() { Alignment = System.Drawing.ContentAlignment.TopLeft, Text = "Total", Name = "Total" });
        chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
        chart.Height = new System.Web.UI.WebControls.Unit(300, System.Web.UI.WebControls.UnitType.Pixel);
        chart.ImageStorageMode = ImageStorageMode.UseHttpHandler;
        ChartArea area = new ChartArea("ChartArea1");
        area.BorderColor = Color.Transparent;
        area.BackSecondaryColor = Color.White;
        area.ShadowColor = Color.FromArgb(64, 64, 64, 64);
        area.BackColor = Color.Transparent;
        area.BackGradientStyle = GradientStyle.TopBottom;
        area.AxisX.MajorGrid.Interval = 1;
        area.AxisX.MajorTickMark.Interval = 1;
        area.AxisX.LabelStyle.Interval = 1;
        area.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
        Font font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold);
        area.AxisY.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.MajorGrid.LineColor = Color.Transparent;

        area.AxisX.IsMarksNextToAxis = true;
        area.AxisX.MajorGrid.LineColor = Color.Transparent;
        area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LineColor = Color.Transparent;
        area.AxisY.LineColor = Color.Transparent;

        area.AxisY.LabelStyle = new LabelStyle() { Enabled = true };
        area.AxisX.LabelStyle = new LabelStyle() { Enabled = false };
        area.AxisY.MajorTickMark = new TickMark() { Enabled = false };
        area.AxisX.MajorTickMark = new TickMark() { Enabled = false };

        ChartArea3DStyle area3d = new ChartArea3DStyle();
        area3d.Inclination = 70;
        area3d.IsClustered = true;
        area3d.PointGapDepth = 10;
        area3d.PointDepth = 25;
        area3d.Enable3D = true;
        area3d.IsRightAngleAxes = false;
        area.Area3DStyle = area3d;
        chart.ChartAreas.Add(area);

        Legend legend = new Legend("legend");
        legend.BorderColor = Color.Transparent;
        legend.Docking = Docking.Bottom;
        legend.IsDockedInsideChartArea = true;
        legend.TableStyle = LegendTableStyle.Wide;
        legend.Alignment = StringAlignment.Near;
        chart.Legends.Clear();
        chart.Legends.Add(legend);


        Series series1 = new Series("promoter");
        series1.ChartType = SeriesChartType.StackedBar;
        series1.Color = C_Green;
        series1.BorderWidth = 0;
        series1.ShadowOffset = 2;

        Series series2 = new Series("passive");
        series2.ChartType = SeriesChartType.StackedBar;
        series2.Color = C_Yellow;
        series2.BorderWidth = 0;
        series2.ShadowOffset = 2;

        Series series3 = new Series("detractor");
        series3.ChartType = SeriesChartType.StackedBar;
        series3.Color = C_Red;
        series3.BorderWidth = 0;
        series3.ShadowOffset = 2;

        //fill chart content
        int top = 0;
        int total = 0;
        DataTable dt = ncc.getSummaryTotal(year);
        foreach (DataRow dr in dt.Rows)
        {
            string title = dr["title"].ToString();
            if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
            {
                total += Convert.ToInt32(dr["total"].ToString());
            }
        }
        ltTotal.Text = "Number of respondent : " + total.ToString();
        foreach (DataRow dr in dt.Rows)
        {
            string title = dr["title"].ToString();
            if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
            {
                int data = Convert.ToInt32(dr["total"].ToString());
                DataPoint point = new DataPoint()
                {
                    Label = getpercent(Convert.ToDouble(data), Convert.ToDouble(total)).ToString(),
                    YValues = new double[] { data }
                };
                if (title.Equals(type_Promoter))
                {
                    series1.Points.Add(point);
                }
                else if (title.Equals(type_Passive))
                {
                    series2.Points.Add(point);
                }
                else { series3.Points.Add(point); }

                if (top < data) { top = data; }
            }
        }

        if (isPromotore)
        {
            if (series1.Points.Count == 0)
            {
                series1.Points.Add(new DataPoint() { IsEmpty = true });
            }
            chart.Series.Add(series1);
        } if (isPassive)
        {
            if (series2.Points.Count == 0)
            {
                series2.Points.Add(new DataPoint() { IsEmpty = true });
            }
            chart.Series.Add(series2);
        } if (isDetractor)
        {
            if (series3.Points.Count == 0)
            {
                series3.Points.Add(new DataPoint() { IsEmpty = true });
            }
            chart.Series.Add(series3);
        }
        //chart.ChartAreas[0].AxisY.Maximum = top;
        //pChart.Controls.Add(chart);
        chart.ChartAreas[0].AxisY.Maximum = top;
        MemoryStream stream = new MemoryStream();
        chart.SaveImage(stream, ChartImageFormat.Png);
        pChart.Controls.Add(new System.Web.UI.WebControls.Image() { ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray()) });

    }

    private void getClassSummary(int year, bool isPromotore, bool isPassive, bool isDetractor)
    {
        pChart.Controls.Clear();
        NPSCustomerContactController ncc = new NPSCustomerContactController();

        //chart initialize
        Chart chart = new Chart();
        //chart.ImageLocation = "~/tempImages/ChartPic_#SEQ(300,3)";
        chart.ImageType = ChartImageType.Png;
        chart.Titles.Add(new Title() { Alignment = System.Drawing.ContentAlignment.TopLeft, Text = "Class", Name = "Class" });
        chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
        chart.Height = new System.Web.UI.WebControls.Unit(500, System.Web.UI.WebControls.UnitType.Pixel);
        chart.ImageStorageMode = ImageStorageMode.UseHttpHandler;
        ChartArea area = new ChartArea("ChartArea1");
        area.BorderColor = Color.Transparent;
        area.BackSecondaryColor = Color.White;
        area.ShadowColor = Color.FromArgb(64, 64, 64, 64);
        area.BackColor = Color.Transparent;
        area.BackGradientStyle = GradientStyle.TopBottom;
        area.AxisX.MajorGrid.Interval = 1;
        area.AxisX.MajorTickMark.Interval = 1;
        area.AxisX.Interval = 1;
        area.AxisX.LabelStyle.Interval = 1;
        area.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
        Font font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold);
        area.AxisY.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.MajorGrid.LineColor = Color.Transparent;

        area.AxisX.IsMarksNextToAxis = true;
        area.AxisX.MajorGrid.LineColor = Color.Transparent;
        area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LineColor = Color.Transparent;
        area.AxisY.LineColor = Color.Transparent;

        area.AxisY.LabelStyle = new LabelStyle() { Enabled = true };
        area.AxisY.MajorTickMark = new TickMark() { Enabled = false };
        area.AxisX.MajorTickMark = new TickMark() { Enabled = false };

        ChartArea3DStyle area3d = new ChartArea3DStyle();
        area3d.Inclination = 70;
        area3d.IsClustered = true;
        area3d.PointGapDepth = 10;
        area3d.PointDepth = 25;
        area3d.Enable3D = true;
        area3d.IsRightAngleAxes = false;
        area.Area3DStyle = area3d;
        chart.ChartAreas.Add(area);

        Legend legend = new Legend("legend");
        legend.BorderColor = Color.Transparent;
        legend.Docking = Docking.Bottom;
        legend.IsDockedInsideChartArea = true;
        legend.TableStyle = LegendTableStyle.Wide;
        legend.Alignment = StringAlignment.Near;
        chart.Legends.Clear();
        chart.Legends.Add(legend);

        Series series1 = new Series("promoter");
        series1.ChartType = SeriesChartType.StackedBar;
        series1.Color = C_Green;
        series1.BorderWidth = 3;
        series1.ShadowOffset = 2;

        Series series2 = new Series("passive");
        series2.ChartType = SeriesChartType.StackedBar;
        series2.Color = C_Yellow;
        series2.BorderWidth = 3;
        series2.ShadowOffset = 2;

        Series series3 = new Series("detractor");
        series3.ChartType = SeriesChartType.StackedBar;
        series3.Color = C_Red;
        series3.BorderWidth = 3;
        series3.ShadowOffset = 2;

        //fill chart content
        int top = 0;

        int total = 0;
        int yindex = 1;
        DataTable dtClass = ncc.getClassSummary();
        foreach (DataRow row in dtClass.Rows)
        {
            int id = Convert.ToInt32(row["p_customer_class_ID"].ToString());
            string name = row["code"].ToString();
            DataTable dt = ncc.getSummaryClass(year, id);


            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    total += Convert.ToInt32(dr["total"].ToString());
                }
            }
            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    int data = Convert.ToInt32(dr["total"].ToString());
                    DataPoint point = new DataPoint()
                            {
                                Label = getpercent(Convert.ToDouble(data), Convert.ToDouble(total)).ToString(),
                                YValues = new double[] { data },
                                AxisLabel = name
                            };
                    if (title.Equals(type_Promoter))
                    {
                        series1.Points.Add(point);
                    }
                    else if (title.Equals(type_Passive))
                    {
                        series2.Points.Add(point);
                    }
                    else { series3.Points.Add(point); }

                    if (top < data) { top = data; }
                }
            }
            if (series1.Points.Count < yindex)
            {
                series1.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series2.Points.Count < yindex)
            {
                series2.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series3.Points.Count < yindex)
            {
                series3.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            yindex++;
        }
        int height = initialheight + ((yindex - 1) * intervalheight);
        chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
        chart.Height = new System.Web.UI.WebControls.Unit(height, System.Web.UI.WebControls.UnitType.Pixel);
        if (isPromotore)
        {
            chart.Series.Add(series1);
        } if (isPassive)
        {
            chart.Series.Add(series2);
        } if (isDetractor)
        {
            chart.Series.Add(series3);
        }
        ltTotal.Text = "Number of respondent : " + total.ToString();
        chart.ChartAreas[0].AxisY.Maximum = top;
        MemoryStream stream = new MemoryStream();
        chart.SaveImage(stream, ChartImageFormat.Png);
        pChart.Controls.Add(new System.Web.UI.WebControls.Image() { ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray()) });

    }

    private void getSegmentSummary(int year, bool isPromotore, bool isPassive, bool isDetractor)
    {
        pChart.Controls.Clear();
        NPSCustomerContactController ncc = new NPSCustomerContactController();

        //chart initialize
        Chart chart = new Chart();
        //chart.ImageLocation = "~/tempImages/ChartPic_#SEQ(300,3)";
        chart.ImageType = ChartImageType.Png;
        chart.Titles.Add(new Title() { Alignment = System.Drawing.ContentAlignment.TopLeft, Text = "Segment", Name = "Segment" });
        chart.ImageStorageMode = ImageStorageMode.UseHttpHandler;
        ChartArea area = new ChartArea("ChartArea1");
        area.BorderColor = Color.Transparent;
        area.BackSecondaryColor = Color.White;
        area.ShadowColor = Color.FromArgb(64, 64, 64, 64);
        area.BackColor = Color.Transparent;
        area.BackGradientStyle = GradientStyle.TopBottom;
        area.AxisX.MajorGrid.Interval = 1;
        area.AxisX.MajorTickMark.Interval = 1;
        area.AxisX.LabelStyle.Interval = 1;
        area.AxisX.Interval = 1;
        area.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
        Font font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold);
        area.AxisY.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.MajorGrid.LineColor = Color.Transparent;

        area.AxisX.IsMarksNextToAxis = true;
        area.AxisX.MajorGrid.LineColor = Color.Transparent;
        area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LineColor = Color.Transparent;
        area.AxisY.LineColor = Color.Transparent;

        area.AxisY.LabelStyle = new LabelStyle() { Enabled = true };
        area.AxisY.MajorTickMark = new TickMark() { Enabled = false };
        area.AxisX.MajorTickMark = new TickMark() { Enabled = false };

        ChartArea3DStyle area3d = new ChartArea3DStyle();
        area3d.Inclination = 70;
        area3d.IsClustered = true;
        area3d.PointGapDepth = 10;
        area3d.PointDepth = 25;
        area3d.Enable3D = true;
        area3d.IsRightAngleAxes = false;
        area.Area3DStyle = area3d;
        chart.ChartAreas.Add(area);

        Legend legend = new Legend("legend");
        legend.BorderColor = Color.Transparent;
        legend.Docking = Docking.Bottom;
        legend.IsDockedInsideChartArea = true;
        legend.TableStyle = LegendTableStyle.Wide;
        legend.Alignment = StringAlignment.Near;
        chart.Legends.Clear();
        chart.Legends.Add(legend);

        Series series1 = new Series("promoter");
        series1.ChartType = SeriesChartType.StackedBar;
        series1.Color = C_Green;
        series1.BorderWidth = 3;
        series1.ShadowOffset = 2;

        Series series2 = new Series("passive");
        series2.ChartType = SeriesChartType.StackedBar;
        series2.Color = C_Yellow;
        series2.BorderWidth = 3;
        series2.ShadowOffset = 2;

        Series series3 = new Series("detractor");
        series3.ChartType = SeriesChartType.StackedBar;
        series3.Color = C_Red;
        series3.BorderWidth = 3;
        series3.ShadowOffset = 2;

        //fill chart content
        int top = 0;

        int total = 0;
        int yindex = 1;
        DataTable dtClass = ncc.getSegmentSummary(year);
        foreach (DataRow row in dtClass.Rows)
        {
            int id = Convert.ToInt32(row["p_company_segment_ID"].ToString());
            string name = row["code"].ToString();
            DataTable dt = ncc.getSummarySegment(year, id);
            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    total += Convert.ToInt32(dr["total"].ToString());
                }
            }


            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    int data = Convert.ToInt32(dr["total"].ToString());
                    DataPoint point = new DataPoint()
                    {
                        Label = getpercent(Convert.ToDouble(data), Convert.ToDouble(total)).ToString(),
                        YValues = new double[] { data },
                        AxisLabel = name
                    };
                    if (title.Equals(type_Promoter))
                    {
                        series1.Points.Add(point);
                    }
                    else if (title.Equals(type_Passive))
                    {
                        series2.Points.Add(point);
                    }
                    else { series3.Points.Add(point); }
                    if (top < data) { top = data; }
                }
            }
            if (series1.Points.Count < yindex)
            {
                series1.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series2.Points.Count < yindex)
            {
                series2.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series3.Points.Count < yindex)
            {
                series3.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            yindex++;
        }
        int height = initialheight + ((yindex - 1) * intervalheight);
        chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
        chart.Height = new System.Web.UI.WebControls.Unit(height, System.Web.UI.WebControls.UnitType.Pixel);
        if (isPromotore)
        {
            chart.Series.Add(series1);
        } if (isPassive)
        {
            chart.Series.Add(series2);
        } if (isDetractor)
        {
            chart.Series.Add(series3);
        } ltTotal.Text = "Number of respondent : " + total.ToString();
        chart.ChartAreas[0].AxisY.Maximum = top;
        MemoryStream stream = new MemoryStream();
        chart.SaveImage(stream, ChartImageFormat.Png);
        pChart.Controls.Add(new System.Web.UI.WebControls.Image() { ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray()) });
    }

    private void getCompanySummary(int year, bool isPromotore, bool isPassive, bool isDetractor)
    {
        pChart.Controls.Clear();
        NPSCustomerContactController ncc = new NPSCustomerContactController();

        //chart initialize
        Chart chart = new Chart();
        //chart.ImageLocation = "~/tempImages/ChartPic_#SEQ(300,3)";
        chart.ImageType = ChartImageType.Png;
        
        chart.Titles.Add(new Title() { Alignment = System.Drawing.ContentAlignment.TopLeft, Text = "Segment", Name = "Segment" });
        chart.ImageStorageMode = ImageStorageMode.UseHttpHandler;
        ChartArea area = new ChartArea("ChartArea1");
        area.BorderColor = Color.Transparent;
        area.BackSecondaryColor = Color.White;
        area.ShadowColor = Color.FromArgb(64, 64, 64, 64);
        area.BackColor = Color.Transparent;
        area.BackGradientStyle = GradientStyle.TopBottom;
        area.AxisX.MajorGrid.Interval = 1;
        area.AxisX.MajorTickMark.Interval = 1;
        area.AxisX.LabelStyle.Interval = 1;
        area.AxisX.Interval = 1;
        area.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
        Font font = new Font("Trebuchet MS", 8.25f, FontStyle.Bold);
        area.AxisY.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LabelStyle = new LabelStyle() { Font = font };
        area.AxisX.MajorGrid.LineColor = Color.Transparent;

        area.AxisX.IsMarksNextToAxis = true;
        area.AxisX.MajorGrid.LineColor = Color.Transparent;
        area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
        area.AxisX.LineColor = Color.Transparent;
        area.AxisY.LineColor = Color.Transparent;

        area.AxisY.LabelStyle = new LabelStyle() { Enabled = true };
        area.AxisY.MajorTickMark = new TickMark() { Enabled = false };
        area.AxisX.MajorTickMark = new TickMark() { Enabled = false };

        ChartArea3DStyle area3d = new ChartArea3DStyle();
        area3d.Inclination = 70;
        area3d.IsClustered = true;
        area3d.PointGapDepth = 10;
        area3d.PointDepth = 25;
        area3d.Enable3D = true;
        area3d.IsRightAngleAxes = false;
        area.Area3DStyle = area3d;
        chart.ChartAreas.Add(area);

        Legend legend = new Legend("legend");
        legend.BorderColor = Color.Transparent;
        legend.Docking = Docking.Bottom;
        legend.IsDockedInsideChartArea = true;
        legend.TableStyle = LegendTableStyle.Wide;
        legend.Alignment = StringAlignment.Near;
        chart.Legends.Clear();
        chart.Legends.Add(legend);

        Series series1 = new Series("promoter");
        series1.ChartType = SeriesChartType.StackedBar;
        series1.Color = C_Green;
        series1.BorderWidth = 3;
        series1.ShadowOffset = 2;

        Series series2 = new Series("passive");
        series2.ChartType = SeriesChartType.StackedBar;
        series2.Color = C_Yellow;
        series2.BorderWidth = 3;
        series2.ShadowOffset = 2;

        Series series3 = new Series("detractor");
        series3.ChartType = SeriesChartType.StackedBar;
        series3.Color = C_Red;
        series3.BorderWidth = 3;
        series3.ShadowOffset = 2;

        //fill chart content
        int top = 0;

        int total = 0;
        DataTable dtClass = ncc.getCompanySummary(year);
        int yindex = 1;
        foreach (DataRow row in dtClass.Rows)
        {
            int id = Convert.ToInt32(row["company_ID"].ToString());
            string name = row["company_name"].ToString();
            DataTable dt = ncc.getSummaryCompany(year, id);
            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    total += Convert.ToInt32(dr["total"].ToString());
                }
            }


            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["title"].ToString();
                if ((isPromotore && title.Equals(type_Promoter)) || (isPassive && title.Equals(type_Passive)) || (isDetractor && title.Equals(type_Detractor)))
                {
                    int data = Convert.ToInt32(dr["total"].ToString());
                    DataPoint point = new DataPoint()
                    {
                        Label = getpercent(Convert.ToDouble(data), Convert.ToDouble(total)).ToString(),
                        YValues = new double[] { data },
                        AxisLabel = name
                    };
                    if (title.Equals(type_Promoter))
                    {
                        series1.Points.Add(point);
                    }
                    else if (title.Equals(type_Passive))
                    {
                        series2.Points.Add(point);
                    }
                    else { series3.Points.Add(point); }
                    if (top < data) { top = data; }
                }
            }
            if (series1.Points.Count < yindex)
            {
                series1.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series2.Points.Count < yindex)
            {
                series2.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            if (series3.Points.Count < yindex)
            {
                series3.Points.Add(new DataPoint() { IsEmpty = true, AxisLabel = name });
            }
            yindex++;

        }
        int height = initialheight + ((yindex - 1) * intervalheight);
        chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
        chart.Height = new System.Web.UI.WebControls.Unit(height, System.Web.UI.WebControls.UnitType.Pixel);

        if (isPromotore)
        {
            chart.Series.Add(series1);
        } if (isPassive)
        {
            chart.Series.Add(series2);
        } if (isDetractor)
        {
            chart.Series.Add(series3);
        }
        ltTotal.Text = "Number of respondent : " + total.ToString();
        chart.ChartAreas[0].AxisY.Maximum = top;
        MemoryStream stream = new MemoryStream();
        chart.SaveImage(stream, ChartImageFormat.Png);
        pChart.Controls.Add(new System.Web.UI.WebControls.Image() { ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray()) });
    }

    private void getReasonSummary(int year, int type)
    {
        pChart.Controls.Clear();
        NPSReasonCategoryController rcc = new NPSReasonCategoryController();
        NPSReasonController rc = new NPSReasonController();
        NPSSurveyController sc = new NPSSurveyController();

        //load total
        int totals = sc.NPSSurveyList.Where(x => x.dtmSurveyDate.Year.ToString().Equals(ddlBatch.SelectedValue) && x.iNPSTypeID.Equals(type)).Count();
        ltTotal.Text = "Number of respondent : " + totals.ToString();

        //loadchart
        List<Entities.NPSReasonCategory> categories = rcc.NPSReasonCategoryList;
        List<Chart> charts = new List<Chart>();
        int top = 0;
        foreach (Entities.NPSReasonCategory category in categories)
        {
            List<Entities.NPSReason> reasons = rc.NPSReasonList.Where(x => x.iNPSReasonCategoryID.Equals(category.iID)).ToList();

            Chart chart = new Chart();
            //chart.ImageLocation = "~/tempImages/ChartPic_#SEQ(300,3)";
            chart.ImageType = ChartImageType.Png;
            chart.Titles.Add(new Title() { Alignment = System.Drawing.ContentAlignment.TopLeft, Text = category.sxTitle, Name = category.sxTitle });

            chart.ImageStorageMode = ImageStorageMode.UseHttpHandler;
            ChartArea area = new ChartArea("ChartArea1");
            area.InnerPlotPosition = new ElementPosition(50, 0, 50, 80);
            //area.BorderColor = Color.FromArgb(64, 64, 64, 64);
            area.BackSecondaryColor = Color.White;
            area.ShadowColor = Color.Transparent;
            area.BackColor = Color.FromArgb(64, 165, 191, 228);
            area.BackGradientStyle = GradientStyle.TopBottom;
            area.AxisX.MajorGrid.Interval = 1;


            area.AxisX.MajorGrid.LineColor = Color.Transparent;
            area.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            area.AxisX.LineColor = Color.Transparent;
            area.AxisY.LineColor = Color.Transparent;
            area.AxisY.LabelStyle = new LabelStyle() { Enabled = false };
            area.AxisY.MajorTickMark = new TickMark() { Enabled = false };
            area.AxisX.MajorTickMark = new TickMark() { Enabled = false };

            chart.ChartAreas.Add(area);

            Series series = new Series(category.sxTitle);
            series.ChartType = SeriesChartType.Bar;
            series.Color = C_Purple;
            series.BorderWidth = 0;
            series.ShadowOffset = 3;
            int counter = 0;
            foreach (Entities.NPSReason reason in reasons)
            {
                int total = sc.NPSSurveyList.Where(x => x.dtmSurveyDate.Year == year && x.iNPSReasonID.Equals(reason.iID) && x.iNPSTypeID.Equals(type)).Count();
                series.Points.Add(new DataPoint()
                {
                    Label = total.ToString(),
                    YValues = new double[] { total },
                    AxisLabel = reason.sxTitle
                });
                if (top < total) { top = total; }
                counter++;
            }
            chart.Series.Add(series);
            int height = 20 + (counter * 40);
            chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
            chart.Height = new System.Web.UI.WebControls.Unit(height, System.Web.UI.WebControls.UnitType.Pixel);

            charts.Add(chart);
        }

        foreach (Chart ch in charts)
        {
            ch.ChartAreas[0].AxisY.Maximum = top;
            MemoryStream stream = new MemoryStream();
            ch.SaveImage(stream, ChartImageFormat.Png);
            pChart.Controls.Add(new System.Web.UI.WebControls.Image() { ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray()) });
        }
    }

    protected void ddlBatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        showChart();
    }

    protected void rblShow_SelectedIndexChanged(object sender, EventArgs e)
    {
        showChart();
    }

    protected void chkShow_SelectedIndexChanged(object sender, EventArgs e)
    {
        showChart();
    }

    protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadPanel(ddlView.SelectedValue.ToString());
        showChart();
    }

    private string getpercent(double data, double total)
    {
        return Math.Round((data * 100) / total).ToString() + " %";
    }

    protected static string V_Reason = "Reason";
    protected static string V_Class = "Class";
    protected static string V_Segment = "Segment";
    protected static string V_Company = "Company";
    protected static string V_Total = "Total";

    protected static Color C_Green
    {
        get { return Color.FromArgb(15, 166, 74); }
    }

    protected static Color C_Yellow
    {
        get { return Color.FromArgb(255, 212, 62); }
    }

    protected static Color C_Red
    {
        get { return Color.FromArgb(169, 30, 34); }
    }

    protected static Color C_Purple
    {
        get { return Color.FromArgb(140, 68, 153); }
    }
}
